let api = require('../../api/api.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    animationState: false,
    animationStates: true,
    tokenState: false,
    isToken:false
  },
  //立即领取
  lingqu:function(){
    // let token = wx.getStorageSync("token")
    // console.log(token)
    // let that = this
    // if (token) {
    //   this.setData({
    //     tokenState: false
    //   })
    //   let userInfo = app.globalData.userInfo
    //   console.log(userInfo)
    //   var ids = ['18', '20'];
    //   let data = [{
    //     id: ids,
    //     user_name: userInfo.nickName,
    //     user_img: userInfo.avatarUrl,
    //     openid_wechat: wx.getStorageSync("key").openid,
    //     level: app.globalData.level
    //   }]
    //   data = JSON.stringify(data);
    //   api.newregister(data, (res) => {
    //     console.log(res)
    //     wx.setStorageSync('token', res.data.result.token)
    //     if (res.data.result.type == 2) {
    //       wx.navigateTo({
    //         url: '/pages/my/coupon/coupon',
    //       })
    //     } else {
    //       that.setData({
    //         animationState: false,
    //         animationStates: false
    //       })
    //       wx.showToast({
    //         icon:'none',
    //         title: '您不是新人，不能领取该优惠劵哦~'
    //       })
    //     }
    //   })
    // } else {
      
    // }
    this.setData({
      tokenState: true
    })
  },

  // 授权
  onGotUserInfo: function (e) {
    var S = this;
    wx.getUserInfo({
      success: function (res) {
        let userInfo = e.detail.userInfo
        var ids = ['18','20'];
        console.log(userInfo)
        let data = [{
          id:ids,
          user_name: e.detail.userInfo.nickName,
          user_img: e.detail.userInfo.avatarUrl,
          openid_wechat: wx.getStorageSync("key").openid,
          level: app.globalData.level
        }]
        data = JSON.stringify(data);
        console.log(data)
        api.newregister(data, (res) => {
          console.log(res)
          wx.setStorageSync('token', res.data.result.token)
          S.setData({
            tokenState: false,
            
          })
          if(res.data.result.type == 2){
            wx.navigateTo({
              url: '/pages/my/coupon/coupon',
            })
          }else{
            S.setData({
              animationState:false,
              animationStates:false
            })
            wx.showToast({
              icon: 'none',
              title: '您不是新人，不能领取该优惠劵哦~'
            })
          }
        })
      },
      fail: function(err) {
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  url: function(e) {

    let id = e.currentTarget.dataset.id
    console.log(id)

    if (id == 1000) {
      return false
    } else if (id == 1001) {
      wx.navigateTo({
        url: `/pages/xiangqing/xiangqing`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/buy/buyConten/buyConten?id=${id}`,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const scene = decodeURIComponent(options.scene)
    api.coupon(options, res => {
      console.log(res)
      this.setData({
        coupon: res.data.result
      })
    })
    let that = this;
    let token = wx.getStorageSync("token")
    let isToken = that.data.isToken
    console.log(token)
    if (token != undefined || token != "") {
      console.log("token存在")
      let data = {}
      data.token = token
      api.newindexs(data, res => {
        this.setData({
          indexData: res.data.result,
          
        })
      })
      api.xin_token(data, res => {
        console.log(res)
        if(res.data.result.type == 1){
          isToken = true
        }else{
          isToken = false
        }
        console.log(isToken)
        this.setData({
          isToken: isToken
        })
      })
    } else {
      console.log("token不存在")
      api.index(res => {
        console.log(res)
        this.setData({
          indexData: res.data.result,
          isToken:false
        })
      })
    }
    api.animation(res => {
      console.log(res)
      this.setData({
        animation: res.data.result,
        animationState: true
      })
      this.setIn = setInterval(res => {
        this.setData({
          animationState: false
        })
      }, 12000)
    })
    if(scene){
      app.globalData.level = scene
    }

    // app.globalData.user_level = user_scene
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(data) {},
  closeLayer: function() {
    this.setData({
      animationState: false,
      animationStates:false
    })
    clearInterval(this.setIn)
  },





  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})